package Dao.DaoImpl;

import Dao.systemMsgDao;
import Utils.JDBCUtils;
import domain.systemMsg;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class systemMsgDaoImpl implements systemMsgDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<systemMsg> findNewMsgByUsername(String username) {
        String sql = "select * from systemmsg where username=? and isread='false'";

        return template.query(sql,new BeanPropertyRowMapper<>(systemMsg.class),username);
    }

    @Override
    public List<systemMsg> findHistoryMsgByUsername(String username) {
        String sql ="select * from systemmsg where username=? and isread='true'";
        return template.query(sql,new BeanPropertyRowMapper<>(systemMsg.class),username);
    }

    @Override
    public void addMsg(systemMsg msg) {
        String sql ="insert into systemmsg values(null,?,?,?,?,?)";
        template.update(sql,msg.getUsername(),msg.getTitle(),msg.getNeirong(),msg.getSendTime(),msg.getIsread());
    }

    @Override
    public systemMsg findMsgByid(int id) {
        String sql ="select * from systemmsg where id=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(systemMsg.class),id);
    }

    @Override
    public void changeMsgIsread(String flag,int id) {
        String sql ="update systemmsg set isread=? where id=?";
         template.update(sql,flag,id);
    }
}
